package com.controller;

import com.entity.SignTask;
import com.form.signTask.PageListSignTask;
import com.from.signTask.AddSignTaskFrom;
import com.service.SignTaskService;
import com.vo.Msg;
import com.vo.PageVo;
import com.vo.signTask.DisplaySignTask;
import com.vo.signTask.SignTaskDetail;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * Version: V1.0    <br/>
 * Datetime:   2022/5/13 22:12   <br/>
 * Description: 签到任务控制器
 *
 * @author: chen
 */
@RestController
@RequestMapping(value = "/signTask")
public class SignTaskController {

    private SignTaskService signTaskService;

    @PostMapping("/add")
    public Msg addSignTask(@RequestBody AddSignTaskFrom addFrom) {
        addFrom.setId(null);
        signTaskService.addSignTask(addFrom);
        return Msg.success();
    }

    /**
     * 分页查询签到任务
     */
    @GetMapping("/pageList")
    public Msg pageListSignTask(PageListSignTask pageListSignTask) throws ParseException {
        PageVo<DisplaySignTask> pageVo = signTaskService.pageListSignTask(pageListSignTask);
        return Msg.success(pageVo);
    }

    @DeleteMapping("/deleteSignTask")
    public Msg deleteSignTask(Long id) {
        signTaskService.deleteSignTask(id);
        return Msg.success();
    }

    /**
     * 根据id查询签到任务
     *
     * @param id 签到任务id
     * @return 签到任务详情
     */
    @GetMapping("/getSignTaskDetail")
    public Msg<SignTaskDetail> getSignTaskDetail(Long id) {
        return Msg.success(signTaskService.getSignTaskDetail(id));
    }

    @PostMapping("/update")
    public Msg updateSignTask(@RequestBody AddSignTaskFrom addFrom) {
        if (addFrom.getId() == null) {
            return Msg.error("id不能为空");
        }
        signTaskService.modifySignTask(addFrom);
        return Msg.success();
    }

    @PostMapping("/sign")
    public Msg signIn(@RequestBody SignTaskForm signTaskForm) {
        if (signTaskForm == null || signTaskForm.getUserId() == null || signTaskForm.getSignTaskId() == null) {
            return Msg.error("参数不能为空");
        }

        return signTaskService.signIn(signTaskForm.getUserId(), signTaskForm.signTaskId);
    }

    @GetMapping("/getSignDetail")
    public Msg getSignDetail(Long id) {
        return Msg.success(signTaskService.getSignDetail(id));
    }

    /**
     * 获取用户能签到的签到列表
     */
    @GetMapping("/getStuSignList")
    public Msg getStuSignList(Long userId) {
        if (userId == null) {
            return Msg.error("用户id不能为空");
        }
        return Msg.success(signTaskService.getStuSignList(userId));
    }

    @Autowired
    public void setSignTaskService(SignTaskService signTaskService) {
        this.signTaskService = signTaskService;
    }

    @Data
    private static class SignTaskForm {
        private Long userId;
        private Long signTaskId;
    }
}